function Contact_Code() {
	var buffer = new StringBuffer();
	buffer.append('<div id="find" class="finds"><div class="searchinput ui-border-radius"><input id="keyword" class="bordersolid" type="text" value="" onblur="Show_Search()" onkeyup="Contact_Search();"/></div><div id="searchicon" class="searchicon"><img src="img/jing.jpg"/></div><div id="searchicons" class="searchicons"><img src="img/jings.jpg"/></div><div class="screen"><button id="screen" class="ui-border-radius">筛选</button></div></div>');
	buffer.append('<div class="horizen border-bottom"></div>');
	return buffer.toString();
};

function Init_Contact_Code()
{
	get("find").addEventListener("tap",function(){
		$("#searchicon").hide();
	    $("#searchicons").show();
	    $("#keyword").attr("placeholder","联系人姓名(支持拼音)，手机");
		if($("keyword").val()!="")
		{
			//WorkMate_Search();
		}
	});
	get("screen").addEventListener("tap",function(){
		OpenScreen(Contact_Search_Code(),function(){
			Init_Contact_Search();
		},Contact_Search);
		stopEvent();
	});
};

function Init_Contact_Search(){
	mui(".option").on("tap",".date",function(){
		obj=this;
			var t=new Date();
			if($(this).val()!="")
			{
				var year_month_day=$(this).val().split("-");
				t.setFullYear(year_month_day[0]);
				t.setMonth(year_month_day[1]-1);
				t.setDate(year_month_day[2]);
			}
			plus.nativeUI.pickDate( function(e){
				var d=e.date;
				$(obj).val((d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()).ToDate());
			},function(e){
				console.log( "未选择日期："+e.message );
			},{date:t});
	});
	
};

function Contact_Search_Code()
{
	var buffer = new StringBuffer();
	buffer.append('<div class="searchline" style="margin-top:10px;"><div class="names">关联状态</div><div class="option"><select id="state_search"><option value="">全部</option><option value="yes">已关联客户</option><option value="no">未关联客户</option></select></div></div>');
	buffer.append('<div class="searchline"><div class="names">创建时间</div><div class="option"><input id="begindate_search" type="text" class="date" placeholder="开始时间" readonly="readonly"/><input id="enddate_search" type="text" class="date" placeholder="结束时间" readonly="readonly"/></div></div>');
	return buffer.toString();
};

function Contact_Data(data) {
	var buffer = new StringBuffer();
	$(data).each(function(i,info){
		var mobile=info.Mobile;
		if(mobile.Trim()==""){mobile=info.Phone;}
		if(mobile.Trim()==""){mobile='<font class="hui">暂无电话</font>';}
		buffer.append('<div class="customerlist border-bottom" id="'+info.ID+'"><div class="customername">'+info.Name+'</div><div class="customerinfo"><div class="state lightblue"><label class="m">'+mobile+'</label></div><div class="lastconntime">'+info.CreateDate.ToTime()+'</div></div></div>');
	});
	return buffer.toString();
};

function Receive_Contact(pageindex,mbegindate,menddate)
{
	var begindate=RUndefined($("#begindate_search").val());
	var enddate=RUndefined($("#enddate_search").val());
	if(mbegindate){begindate=mbegindate;}
	if(menddate){enddate=menddate;}
	var data = {};
			data.act = "Find_Contact";
			data.userid=userid;
			data.comid=comid;
			data.pageindex=pageindex;
			data.source=source;
			data.pagesize=PAGE_SIZE20;
			data.keyword = $("#keyword").val();
		    data.timer = begindate + "～" + enddate;
		    data.state = RUndefined($("#state_search").val());
		    data.type = 1;
		    data.lowers = "";
		    data.sign=GetSign(data.act);
			mui.getJSON(crm_domain, data, function(json) {
				if(json.data.list.length>0)
				{
					if(pageindex==1)
					{
						$("#contentlist1").html(Contact_Data(json.data.list));
					}else{
						$("#contentlist1").append(Contact_Data(json.data.list));
					}
					setTimeout(function(){
						Init_Contact();
						myScroll1.refresh();
					},500); //需要延迟500毫秒加载，否则当数量少时不能下拉
					if(json.data.list.length!=data.pagesize)
					{
						$("#loading1").html("");
					}else{
						$("#loading1").html('<img src="img/loading.gif"/>');
					}
				}else
				{
					
					if(pageindex==1)
					{
						$("#contentlist1").html("");
						$("#loading1").html("主人，没有找到您需要的数据"); 
					}
					else
					{
						$("#loading1").html("没有更多了");
					}
				}
				
         		pageindex = parseInt($("#divmsg1").attr("pageindex"))+1;
         		$("#divmsg1").attr("pageindex",pageindex);
			});
};

function Contact_Search(){
	pageindex=1;
	$("#divmsg1").attr("pageindex","0");
	Receive_Contact(1);
};

function Init_Contact()
{
	var hasclick=false;
	mui("#contentlist1").off("tap",".customerlist");
	mui("#contentlist1").off("tap",".state");
	mui("#contentlist1").off("longtap",".customerlist");
	mui("#contentlist1").on("tap",".customerlist",function(){
		if(hasclick){
			hasclick=false;
			return;
		}
		OpenWindow2(2,"联系人详情","操作",function(){
			ContactTool();
		});
		obj=$(this);
		$("#contentlist2").html(ContactInfo_Code());
		get("contentlist2").addEventListener("tap",function(){
			if($(".customertool").length>0)
			{
				$(".customertool").hide();
			}
		});
		Init_ContactDetail();
	});
	mui("#contentlist1").on("tap",".m",function(){
		if($(this).find("font").html()!="暂无电话")
		{
			hasclick=true;
		}else
		{
			hasclick=false;
		}
		if(hasclick)
		{
			var phone=$(this).html();
						var name=$(this).parent().parent().find(".customername").html();
						var html='<div class="contactdialog"><div id="sms" class="addcontact border-bottom">发短信</div><div id="phone" class="addcontact border-bottom">打电话</div><div id="savephone" class="addcontact">保存至本地通讯录</div></div>';
						OpenMeng3(html,sheight/2);
						get("sms").addEventListener("tap",function(){
							window.location.href="sms:"+phone;
						});
						get("phone").addEventListener("tap",function(){
							window.location.href="tel:"+phone;
						});
						get("savephone").addEventListener("tap",function(){
							plus.contacts.getAddressBook( plus.contacts.ADDRESSBOOK_PHONE, function( addressbook ) {
								// 可通过addressbook进行通讯录操作
								var contact = addressbook.create();
									contact.name = {givenName:name};
									contact.phoneNumbers = [{type:"mobile",value:phone,preferred:true}];
									contact.save( function () {
										CloseMeng3();
										plus.nativeUI.toast("已成功保存至通讯录");
									}, function ( e ) {
										plus.nativeUI.toast("保存失败");
									} );
								}, function ( e ) {
									plus.nativeUI.toast("保存失败");
							});
	
						});
		}
	});
	mui("#contentlist1").on("longtap",".customerlist",function(){
		obj=$(this);
		plus.nativeUI.confirm("确定要删除吗？", function(event){
				if(event.index==1)
				{
					var data = {};
				    data.act = "Delete_Contact";
				    data.ids = obj.attr("id");
				    data.userid=userid;
					data.comid=comid;
					data.source=source;
					data.sign=GetSign(data.act);
				    mui.getJSON(crm_domain, data, function(json) {
				    	plus.nativeUI.toast(json.msg);
						if(json.state)
						{
							FindCrmReport();
							obj.hide(500);
						}
				    });	
				}
			},"提示", ["取消","确认"]); 
	});
};


